/*********************************************************************
 
  (c) copyright Freescale Semiconductor Ltd. 2010
  ALL RIGHTS RESERVED
 
 *********************************************************************
 
 *********************************************************************

  File:				        MMA8452_Read_and_Filter.h
 
  Description:        Read data from MMA8452Q
  
********************************************************************
 Ver 0.0.1	Released Oct.2010
********************************************************************/

/*****************************************************************************
*                                                                           *
* C O N F I D E N T I A L                                                   *
*                                                                           *
* This code is released under the Non-Disclosure Agreement (NDA) between    *
* customer and Freescale, and is allowed to use in Freescale's products     *
* only                                                                      *
*                                                                           *
*****************************************************************************
* THIS  CODE IS ONLY INTENDED AS AN EXAMPLE FOR DEMONSTRATING THE FREESCALE *
* MICROCONTROLLERS.  IT  HAS ONLY BEEN GIVEN A MINIMUM LEVEL OF TEST. IT IS *
* PROVIDED  'AS  SEEN'  WITH  NO  GUARANTEES  AND  NO  PROMISE  OF SUPPORT. *
*****************************************************************************
*                                                                           *
*****************************************************************************
* Freescale  is  not  obligated  to  provide  any  support, upgrades or new *
* releases  of  the Software. Freescale may make changes to the Software at *
* any time, without any obligation to notify or provide updated versions of *
* the  Software  to you. Freescale expressly disclaims any warranty for the *
* Software.  The  Software is provided as is, without warranty of any kind, *
* either  express  or  implied,  including, without limitation, the implied *
* warranties  of  merchantability,  fitness  for  a  particular purpose, or *
* non-infringement.  You  assume  the entire risk arising out of the use or *
* performance of the Software, or any systems you design using the software *
* (if  any).  Nothing  may  be construed as a warranty or representation by *
* Freescale  that  the  Software  or  any derivative work developed with or *
* incorporating  the  Software  will  be  free  from  infringement  of  the *
* intellectual property rights of third parties. In no event will Freescale *
* be  liable,  whether in contract, tort, or otherwise, for any incidental, *
* special,  indirect, consequential or punitive damages, including, but not *
* limited  to,  damages  for  any loss of use, loss of time, inconvenience, *
* commercial loss, or lost profits, savings, or revenues to the full extent *
* such  may be disclaimed by law. The Software is not fault tolerant and is *
* not  designed,  manufactured  or  intended by Freescale for incorporation *
* into  products intended for use or resale in on-line control equipment in *
* hazardous, dangerous to life or potentially life-threatening environments *
* requiring  fail-safe  performance,  such  as  in the operation of nuclear *
* facilities,  aircraft  navigation  or  communication systems, air traffic *
* control,  direct  life  support machines or weapons systems, in which the *
* failure  of  products  could  lead  directly to death, personal injury or *
* severe  physical  or  environmental  damage  (High  Risk Activities). You *
* specifically  represent and warrant that you will not use the Software or *
* any  derivative  work of the Software for High Risk Activities.           *
* Freescale  and the Freescale logos are registered trademarks of Freescale *
* Semiconductor Inc.                                                        *
****************************************************************************/


#ifndef _READ_AND_FILTER_H
#define _READ_AND_FILTER_H

//#include "FSL_data.h" /* include type definations */
//#include "FSL_MMA.h"
#include "MMA8452Q.h"

typedef union {
  struct{
    VINT8 Xdata8;
    VINT8 Ydata8;
    VINT8 Zdata8;
  } Byte;
  VINT8 XYZdata8[3];
} XYZunion8;

extern volatile XYZunion8 _XYZdata8;

#define Xnew8 _XYZdata8.Byte.Xdata8
#define Ynew8 _XYZdata8.Byte.Ydata8
#define Znew8 _XYZdata8.Byte.Zdata8
#define XYZnew8 _XYZdata8.XYZdata8

extern volatile XYZunion8 _XYZavge8;

#define Xavg8 _XYZavge8.Byte.Xdata8
#define Yavg8 _XYZavge8.Byte.Ydata8
#define Zavg8 _XYZavge8.Byte.Zdata8
#define XYZavg8 _XYZavge8.XYZdata8

extern volatile XYZunion8 _XYZrsdl8;

#define Xrsdl8 _XYZrsdl8.Byte.Xdata8
#define Yrsdl8 _XYZrsdl8.Byte.Ydata8
#define Zrsdl8 _XYZrsdl8.Byte.Zdata8
#define XYZrsdl8 _XYZrsdl8.XYZdata8


#define iniMMA8452_CTRL_REG1 0x01

#define LowPassFactor 7 //15//63//31
#define LowPassRsdl 14
#define MMA8452_FIFO_Size 20


VUINT8 MMA8452_Read_XYZ8(VINT8 *x, VINT8 *y, VINT8 *z);
VUINT8 MMA8452_Read_XYZ12(VINT16 *x, VINT16 *y, VINT16 *z);
VUINT8 MMA8452_Reset(void);
VUINT8 FSL_MMA_StStp(VUINT8 reg_value);
void FSL_MMA_Startup_Delay(void);
VUINT8 MMA8452_Data_Is_Ready(void);
void FSL_MMA_LowPassFilter_8(VINT8 * InData, VINT8 * OutData, VINT8 * RsdlData, VUINT8 Factor);

#endif
